JFile = {
    path = "/opt/app/lua/token/",
    timeout = 60*30,
    New = function()
        p = {}

        for k, v in pairs(JFile) do
            p[k] = v
        end
        return p
    end,

    Read = function(token) --读取服务器token
        path = JFile.path
        timeout = JFile.timeout
        local getFile, err = io.open(path..token, "r")
        if getFile == nil then
            return false
        end
        thatTime = getFile:read()
        if ngx.time() - thatTime > timeout then
            getFile:close()
            return false
        end
        getFile:close()
        return true
    end,
    Create = function(ip) --生成token
        path = JFile.path
        str = ngx.time()
        fileName = ngx.md5(ip..str)
        local out = assert(io.open(path..fileName, "w"))
        out:write(str)
        out:close()
        return fileName
    end
}
handleIp = {
    registerUrl = "/index.php?r=data/index",
    registerList = "/opt/app/lua/register/",
    loginUrl = "/index.php?r=data/sure",
    loginPage = "/index.php?r=data/login",
    New = function()
        i = {}
        for k, v in pairs(handleIp) do
            i[k] = v
        end
        return i
    end,
    GetClientIp = function() --获取用户ip
        clientIP = ngx.req.get_headers()["X-Real-IP"]
        if clientIP == nil then
            clientIP = ngx.req.get_headers()["x_forwarded_for"]
        end
        if clientIP == nil then
            clientIP = ngx.var.remote_addr
        end
        if clientIP == nil then
            clientIP = '123456789'
        end
        return clientIP
    end,
    GetCookieToken = function() --获取用户token
        token = ngx.var.cookie_AccessToken
        --AccessToken = handleIp.AccessToken
        --token = ngx.var.cookie_..AccessToken
        if token == nil then
            return false
        end
        return token
    end,
    SetCookieToken = function(tokens) --设置用户token
        ngx.header["Set-Cookie"] = {'AccessToken='..tokens}
        return true
    end,
    RegisterUrl = function() --判断url是否为要注册的url
        registerUrl = handleIp.registerUrl
        if ngx.var.request_uri == registerUrl then
            return true
        else
            return false
        end
    end,
    SetRegister = function() --用户注册
        ngx.req.read_body()
        args = ngx.req.get_post_args()
        local userName = args["name"]
        local userPassword = args["password"]
        registerList = handleIp.registerList
        local userHave, error = io.open(registerList..userName, "r")
        if userHave then
            ngx.say('username had exist!')
            return
        end
        md5Password = ngx.md5(userPassword)
        local openFile = assert(io.open(registerList..userName, "w"))
        openFile:write(md5Password)
        openFile:close()
        local userFile, err = io.open(registerList..userName, "r")
        if userFile == nil then
            ngx.say('fail')
            --ngx.redirect("www.baidu.com")
        else
            ngx.say('success')
            --ngx.redirect("/",301)
        end
    end,
    LoginUrl = function() -- 判断是否为login的url
        loginUrl = handleIp.loginUrl
        if ngx.var.request_uri == loginUrl then
            return true
        else
            return false
        end
    end,
    SetLogin = function() --登录逻辑，获取用户账号密码匹对文件存数位置的账号密码是否一致
        ngx.req.read_body()
        args = ngx.req.get_post_args()
        local userName = args["name"]
        local userPassword = args["password"]
        registerList = handleIp.registerList
        local userLogin, err = io.open(registerList..userName, "r")
        if userLogin == nil then
            return false
        end
        md5Password = ngx.md5(userPassword)
        if userLogin:read() == md5Password then
            userLogin:close()
            return true
        end
        userLogin:close()
        return false
    end,
    GetUrl = function()
        return ngx.var.request_uri
    end

}

handleClient = handleIp.New()
doFile = JFile.New()

--注册用户
if handleClient.RegisterUrl() then
    handleClient.SetRegister()
end
--是否带有合法的AccessToken,及是否超时
clientToken = handleClient.GetCookieToken()
if clientToken then
    if not doFile.Read(clientToken) then
        if handleClient.GetUrl() ~= handleClient.loginPage or handleClient.GetUrl() ~= handleClient.loginUrl then
            ngx.redirect('/index.php?r=data/login')
        end
    end
end
--用户登录
if handleClient.LoginUrl() then
    if handleClient.SetLogin() then
        fileName = doFile.Create(handleClient.GetClientIp())
        if handleClient.SetCookieToken(fileName) then
            ngx.say("login success aaaaaa")
        end
    end
end


